package com.portal.framework.exception;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 
 * 
 * 异常格式化处理
 * 
 * @author 卢烁波
 * @date 2007-3-10
 */
public class ExceptionFormatter
{
    // formatter 通用设置
    private String filter = "com.htf";  //需要捕获的发生异常的类前缀,包括包名称
    private int traceDepth = 0; // 堆栈信息跟踪的深度    private static final Log log = LogFactory.getLog(ExceptionFormatter.class);

    private Exception _e;
    private StackTraceElement _stElement;
    private StackTraceElement[] _stElements;

    private String _className;
    private String _fileName;
    private String _methodName;
    private int _lineNumber;

    public ExceptionFormatter(Exception e)
    {
        _e = e;
        _stElements = getStackElements();
        _stElement = _stElements[0];// 第一个

        if (_stElement != null)
        {
            _fileName = _stElement.getFileName();
            _className = _stElement.getClassName();
            _methodName = _stElement.getMethodName();
            _lineNumber = _stElement.getLineNumber();
        }
    }
    /**
     * add by 袁金锋 2007-06-28
     * @param e
     * @param traceDepth
     */
    public ExceptionFormatter(Exception e,int traceDepth)
    {
    	this.traceDepth=traceDepth;
        _e = e;
        _stElements = getStackElements();
        _stElement = _stElements[0];// 第一个
        if (_stElement != null)
        {
            _fileName = _stElement.getFileName();
            _className = _stElement.getClassName();
            _methodName = _stElement.getMethodName();
            _lineNumber = _stElement.getLineNumber();
        }
    }
    /**
     * 组织错误信息
     * @return
     */
    public String getStackMessage()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("错误原因：").append(_e.getMessage()).append("\n");
        StackTraceElement stElement = null;
        for (int i=0;i<_stElements.length;i++)
        {
            stElement = _stElements[i];
            if (stElement == null)
            {
                break;
            }
            else
            {
                sb.append(" 在 ").append(stElement).append("\n");
            }
        }
        log.error(sb.toString());
        return sb.toString();
    }
    /**
     * 获取堆栈里的错误信息
     * @return
     */
    private StackTraceElement[] getStackElements()
    {
        StackTraceElement[] stElements = _e.getStackTrace();
        StackTraceElement[] returnStElements = new StackTraceElement[traceDepth];
        StackTraceElement stElement = null;
        for (int i=0,j=0; i<stElements.length && j<traceDepth; i++)
        {
            stElement = stElements[i];

            if (stElement.getClassName().startsWith(filter))
            {
                returnStElements[j] = stElement;
                j++;
            }
        }
        return returnStElements;
    }
	public String get_className() {
		return _className;
	}
	public void set_className(String name) {
		_className = name;
	}
	public Exception get_e() {
		return _e;
	}
	public void set_e(Exception _e) {
		this._e = _e;
	}
	public String get_fileName() {
		return _fileName;
	}
	public void set_fileName(String name) {
		_fileName = name;
	}
	public int get_lineNumber() {
		return _lineNumber;
	}
	public void set_lineNumber(int number) {
		_lineNumber = number;
	}
	public String get_methodName() {
		return _methodName;
	}
	public void set_methodName(String name) {
		_methodName = name;
	}
	public StackTraceElement get_stElement() {
		return _stElement;
	}
	public void set_stElement(StackTraceElement element) {
		_stElement = element;
	}
	public StackTraceElement[] get_stElements() {
		return _stElements;
	}
	public void set_stElements(StackTraceElement[] elements) {
		_stElements = elements;
	}
}
